﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace Freshome.m.vms
{
    internal class Majoraws : TabItemate
    {
        private const string _ep = "wsHttp_IMultint";
        private readonly fox.Userole _ur = null;
        private readonly fox.Helproxy<Rmt.IMultint> _hp;
        private BackgroundWorker _ld = new BackgroundWorker();

        public Majoraws(string nm, fox.Userole ur)
            : base(nm, true)
        {
            this._ur = ur;
            this._hp = new fox.Helproxy<Rmt.IMultint>(ur);
            this._ld.DoWork += new DoWorkEventHandler(Load_DoWork);
            this._ld.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Load_WorkerCompleted);
            this.Content = new vws.Majoraws() { DataContext = this };
        }
        public fox.Userole Userole
        {
            get { return this._ur; }
        }
        public fox.Helproxy<Rmt.IMultint> Helproxy
        {
            get { return this._hp; }
        }
        public bool Readonly
        {
            get { return !Enabled; }
        }
        private bool _enabled = false;
        public bool Enabled
        {
            get { return _enabled && this._ur.IsJunior; }
            set
            {
                if (_enabled != value)
                {
                    _enabled = value;
                    OnPropertyChanged("Enabled");
                    OnPropertyChanged("Readonly");
                }
            }
        }
        private ObservableCollection<mds.MeatMaterial> _meat = new ObservableCollection<mds.MeatMaterial>();
        public ObservableCollection<mds.MeatMaterial> P_meat
        {
            get { return this._meat; }
        }
        private ObservableCollection<mds.GreenMaterial> _green = new ObservableCollection<mds.GreenMaterial>();
        public ObservableCollection<mds.GreenMaterial> P_green
        {
            get { return this._green; }
        }
        private ObservableCollection<mds.GrainMaterial> _grain = new ObservableCollection<mds.GrainMaterial>();
        public ObservableCollection<mds.GrainMaterial> P_grain
        {
            get { return this._grain; }
        }
        private ObservableCollection<mds.WaterMaterial> _water = new ObservableCollection<mds.WaterMaterial>();
        public ObservableCollection<mds.WaterMaterial> P_water
        {
            get { return this._water; }
        }
        private ObservableCollection<mds.EggMaterial> _egg = new ObservableCollection<mds.EggMaterial>();
        public ObservableCollection<mds.EggMaterial> P_egg
        {
            get { return this._egg; }
        }
        private ObservableCollection<mds.BeanMaterial> _bean = new ObservableCollection<mds.BeanMaterial>();
        public ObservableCollection<mds.BeanMaterial> P_bean
        {
            get { return this._bean; }
        }
        private ObservableCollection<mds.MilkMaterial> _milk = new ObservableCollection<mds.MilkMaterial>();
        public ObservableCollection<mds.MilkMaterial> P_milk
        {
            get { return this._milk; }
        }
        private ObservableCollection<mds.FruitMaterial> _fruit = new ObservableCollection<mds.FruitMaterial>();
        public ObservableCollection<mds.FruitMaterial> P_fruit
        {
            get { return this._fruit; }
        }
        private ObservableCollection<mds.NutMaterial> _nut = new ObservableCollection<mds.NutMaterial>();
        public ObservableCollection<mds.NutMaterial> P_nut
        {
            get { return this._nut; }
        }
        private ObservableCollection<mds.MushroomMaterial> _mushroom = new ObservableCollection<mds.MushroomMaterial>();
        public ObservableCollection<mds.MushroomMaterial> P_mushroom
        {
            get { return this._mushroom; }
        }
        public override void Loadata()
        {
            if (!_ld.IsBusy)
            {
                _ld.RunWorkerAsync();
                this.Enabled = false;
            }
        }
        // Loading finished
        void Load_WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null) MessageBox.Show(e.Error.Message, "数据服务");
            else
            {
                var rts = e.Result as Rmt.Majoraw[];
                if (rts != null)
                {
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Meat), _meat);
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Green), _green);
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Grain), _grain);
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Water), _water);
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Egg), _egg);
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Bean), _bean);
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Milk), _milk);
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Fruit), _fruit);
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Nut), _nut);
                    fox.Kitools.Refresh(rts.Where(m => m.Category == Rmt.MajorawCateg.Mushroom), _mushroom);
                }
                this.Enabled = true;
            }
        }
        void Load_DoWork(object sender, DoWorkEventArgs e)
        {
            _hp.Asyncall(cln =>
            {
                e.Result = cln.GetMajoraws(false);
            }, new Rmt.MultintClient(_ep));
        }
        private void SaveMajoraws()
        {
            var mjs = (from m in _meat where (m.IsValid && m.Created) || m.Updated select m.Raw)
                .Union(from m in _green where (m.IsValid && m.Created) || m.Updated select m.Raw)
                .Union(from m in _fruit where (m.IsValid && m.Created) || m.Updated select m.Raw)
                .Union(from m in _water where (m.IsValid && m.Created) || m.Updated select m.Raw)
                .Union(from m in _egg where (m.IsValid && m.Created) || m.Updated select m.Raw)
                .Union(from m in _bean where (m.IsValid && m.Created) || m.Updated select m.Raw)
                .Union(from m in _milk where (m.IsValid && m.Created) || m.Updated select m.Raw)
                .Union(from m in _nut where (m.IsValid && m.Created) || m.Updated select m.Raw)
                .Union(from m in _mushroom where (m.IsValid && m.Created) || m.Updated select m.Raw)
                .Union(from m in _grain where (m.IsValid && m.Created) || m.Updated select m.Raw);
            if (mjs.Count() > 0)
            {
                short mid = short.MinValue; // 为新加记录预设Mid
                foreach (var mj in mjs)
                {
                    if(mj.Mid  == default(short)) mj.Mid = mid++;
                    mj.Stamp = mj.Name;
                }
                Dictionary<short, string> rts = null;
                _hp.Syncall(cln =>
                {
                    rts = cln.SaveMajoraws(mjs.ToArray());
                }, new Rmt.MultintClient(_ep));
                if (rts != null)
                {
                    fox.Kitools.Replace<mds.MeatMaterial, Rmt.Majoraw>(rts, _meat.Where(m => (m.IsValid && m.Created) || m.Updated), "肉类主料");
                    fox.Kitools.Replace<mds.GreenMaterial, Rmt.Majoraw>(rts, _green.Where(m => (m.IsValid && m.Created) || m.Updated), "蔬菜主料");
                    fox.Kitools.Replace<mds.GrainMaterial, Rmt.Majoraw>(rts, _grain.Where(m => (m.IsValid && m.Created) || m.Updated), "米面豆类");
                    fox.Kitools.Replace<mds.WaterMaterial, Rmt.Majoraw>(rts, _water.Where(m => (m.IsValid && m.Created) || m.Updated), "水产主料");
                    fox.Kitools.Replace<mds.EggMaterial, Rmt.Majoraw>(rts, _egg.Where(m => (m.IsValid && m.Created) || m.Updated), "禽蛋主料");
                    fox.Kitools.Replace<mds.BeanMaterial, Rmt.Majoraw>(rts, _bean.Where(m => (m.IsValid && m.Created) || m.Updated), "豆制品主料");
                    fox.Kitools.Replace<mds.MilkMaterial, Rmt.Majoraw>(rts, _milk.Where(m => (m.IsValid && m.Created) || m.Updated), "奶制品主料");
                    fox.Kitools.Replace<mds.FruitMaterial, Rmt.Majoraw>(rts, _fruit.Where(m => (m.IsValid && m.Created) || m.Updated), "水果主料");
                    fox.Kitools.Replace<mds.NutMaterial, Rmt.Majoraw>(rts, _nut.Where(m => (m.IsValid && m.Created) || m.Updated), "坚果主料");
                    fox.Kitools.Replace<mds.MushroomMaterial, Rmt.Majoraw>(rts, _mushroom.Where(m => (m.IsValid && m.Created) || m.Updated), "菌菇主料");
                }
            }
        }
        private bool Savable()
        {
            return _meat.Any(m => (m.Created && m.IsValid) || m.Updated)
                || _green.Any(m => (m.Created && m.IsValid) || m.Updated)
                || _fruit.Any(m => (m.Created && m.IsValid) || m.Updated)
                || _water.Any(m => (m.Created && m.IsValid) || m.Updated)
                || _egg.Any(m => (m.Created && m.IsValid) || m.Updated)
                || _bean.Any(m => (m.Created && m.IsValid) || m.Updated)
                || _milk.Any(m => (m.Created && m.IsValid) || m.Updated)
                || _nut.Any(m => (m.Created && m.IsValid) || m.Updated)
                || _mushroom.Any(m => (m.Created && m.IsValid) || m.Updated)
                || _grain.Any(m => (m.Created && m.IsValid) || m.Updated);
        }
        private Magicommand _save;
        private void Saventry()
        {
            SaveMajoraws();
        }
        public Magicommand Save
        {
            get
            {
                if (_save == null) _save = new Magicommand(Properties.Resources.Save + "(_S)", param => this.Saventry(), param => this.Savable());
                return _save;
            }
        }
    }
}
